package jbrowser.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LogPage extends AbstractPage implements ActionListener {

    private static final Logger logger = Logger.getLogger(LogPage.class.getName());
    private static SimpleFormatter formatter = new SimpleFormatter();
    Handler handler;
    String DEFAULT_LOGGER = "";
    Level DEFAULT_LEVEL = Level.INFO;
    String loggerName = DEFAULT_LOGGER;

    /**
     * ul>
     * <li>SEVERE (highest value)
     * <li>WARNING
     * <li>INFO
     * <li>CONFIG
     * <li>FINE
     * <li>FINER
     * <li>FINEST (lowest value)
     * </ul>
     */
    public LogPage() {
        initComponents();
        handler = new Handler() {
            @Override
            public void publish(LogRecord record) {
                textArea.append(formatter.format(record));
            }

            @Override
            public void flush() {
            }

            @Override
            public void close() throws SecurityException {
            }
        };
        handler.setLevel(DEFAULT_LEVEL);
        Logger.getLogger(DEFAULT_LOGGER).addHandler(handler);
        cmbLogLevel.addItem(Level.OFF);
        cmbLogLevel.addItem(Level.SEVERE);
        cmbLogLevel.addItem(Level.WARNING);
        cmbLogLevel.addItem(Level.INFO);
        cmbLogLevel.addItem(Level.CONFIG);
        cmbLogLevel.addItem(Level.FINE);
        cmbLogLevel.addItem(Level.FINER);
        cmbLogLevel.addItem(Level.FINEST);
        cmbLogLevel.addItem(Level.ALL);
        cmbLogLevel.setSelectedItem(Level.INFO);

        cmbLogLevel.addActionListener(this);
        cmbLoggerName.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(cmbLogLevel)) {
            logger.log(Level.INFO, "Old level: {0}", handler.getLevel());
            logger.log(Level.INFO, "New level: {0}", cmbLogLevel.getSelectedItem());
            handler.setLevel((Level) cmbLogLevel.getSelectedItem());
        } else if (e.getSource().equals(cmbLoggerName)) {
            logger.log(Level.INFO, "Old logger: {0}", loggerName);
            Logger.getLogger(loggerName).removeHandler(handler);
            loggerName = cmbLoggerName.getSelectedItem().toString();
            logger.log(Level.INFO, "New logger: {0}", loggerName);
            Logger.getLogger(loggerName).addHandler(handler);
        }
    }

    @Override
    public String getTitle() {
        return "Log";
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        cmbLoggerName = new javax.swing.JComboBox();
        cmbLogLevel = new javax.swing.JComboBox();
        scrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        lblLoggerName = new javax.swing.JLabel();
        lblLogLevel = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        cmbLoggerName.setEditable(true);
        cmbLoggerName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ".", "jbrowser", "jbrowser.gui", "jbrowser.services" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        add(cmbLoggerName, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        add(cmbLogLevel, gridBagConstraints);

        textArea.setColumns(20);
        textArea.setRows(5);
        scrollPane.setViewportView(textArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        add(scrollPane, gridBagConstraints);

        lblLoggerName.setText("Logger Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        add(lblLoggerName, gridBagConstraints);

        lblLogLevel.setText("Log Level");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        add(lblLogLevel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbLogLevel;
    private javax.swing.JComboBox cmbLoggerName;
    private javax.swing.JLabel lblLogLevel;
    private javax.swing.JLabel lblLoggerName;
    private javax.swing.JScrollPane scrollPane;
    javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables
}
